<?php

namespace App\Jobs;

use App\Exceptions\ApiException;
use App\Libraries\msg\Msg;
use Illuminate\Bus\Queueable;
use Illuminate\Queue\SerializesModels;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;

class SendMsg implements ShouldQueue
{
	use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
	
	protected $mobile;
	protected $template_no;
	protected $param;
	/**
	 * Create a new job instance.
	 *
	 * @return void
	 */
	public function __construct($mobile,$template_no,$param)
	{
		//
		$this->mobile = $mobile;
		$this->template_no = $template_no;
		$this->param = $param;
	}
	
	/**
	 * Execute the job.
	 *
	 * @return void
	 */
	public function handle()
	{
		//
		$msg = new Msg();
		$return = $msg->send($this->mobile,$this->template_no,$this->param);
		if(empty($return)){
			throw new ApiException('发送短信失败');
		}
	}
	
	
	/**
	 * Handle a job failure.
	 *
	 * @return void
	 */
	public function failed()
	{
		// Called when the job is failing...
		logger('失败，'.$this->mobile.'发送消息队列失败,时间：'.time2date().'，参数：'.json_encode([$this->template_no,$this->param]));
		send_mail('382820748@qq.com',$this->mobile.'发送消息队列失败,时间：'.time2date());
	}
}
